/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui.listview;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import todomanager.model.Task;
import todomanager.model.TaskManager;
import todomanager.ui.ActionControl;

/**
 * Creates a JList that contains tasks to be done today. This list has a popup
 * menu that allows the user to mark some tasks as done, and thus remove them
 * from the list.
 * 
 * @author
 * Manuel Rebollo
 * @author
 * Nosheen Zaza
 */
public class TaskDisplayList extends JList {

    private JPopupMenu popup;

    public TaskDisplayList(List<Task> tasks) {
        
        setModel(new TaskListModel(tasks));
        setCellRenderer(new TaskItemRenderer());
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setLayoutOrientation(JList.VERTICAL);
        setVisibleRowCount(5);

        popup = new JPopupMenu();

        JMenuItem menuItem = new JMenuItem(
                ActionControl.getInstance().getStringsBundle(
                ).getString("iDidItRemoveIt"));
        popup.add(menuItem);

        // Mark a task as done, and update the underlying model.
        menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Task t = (Task) TaskDisplayList.this.getSelectedValue();
                t.setDone(true);
                TaskManager.getInstance().editTaskStatus(t, true);
                TaskDisplayList.this.revalidate();
                TaskDisplayList.this.repaint();
            }
        });

        // Show the popup menu when appropirate
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if(maybeShowPopup(e)) {
                    TaskDisplayList.this.setSelectedIndex(
                            TaskDisplayList.this.locationToIndex(e.getPoint()));
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);
            }

            private boolean maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popup.show(e.getComponent(),
                            e.getX(), e.getY());
                    return true;
                }
                return false;
            }
        });
    }
}
